﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace frost
{
    public partial class Spelplan : Form
    {
        //private bool menuState = true;
        private bool contPlay = true;
        private bool lost = false;
        private bool won = false;
        private int clientWidth;
        private int clientHeight;

        private Meny menu = new Meny();
        private Timer timer = new Timer();
        private Rectangle image = new Rectangle(0, 0, 40, 50);
        private Spelare player = new Spelare();
        private SpelMatrix playMatrix = new SpelMatrix();
        private Graphics graphics;
        private Graphics imageGraphics;
        private Image backBuffer;
        private Image blackScreen;
         
        


        public Spelplan()
        {
            InitializeComponent();
            Init();
        }

        private void Spelplan_Paint(object sender, PaintEventArgs e)
        {
            //if (menuState)
                menu.Draw(e.Graphics);
        }

        private void Spelplan_MouseMove(object sender, MouseEventArgs e)
        {
            menu.mouseHover(this, e.X, e.Y);
        }

        private void Spelplan_MouseClick(object sender, MouseEventArgs e)
        {
            switch (menu.mouseClicked(this, e.X, e.Y))
            {
                case "start":
                    playGame();
                    
                    break;
                case "exit":
                    Program.ExitApplication();
                    break;
            }
        }

        public void GameLoop()
        {
            while (this.contPlay)
            {
                timer.Reset();
                GameLogic();
                RenderScene();
                Application.DoEvents();
                while (timer.GetTicks() < 50) ;
            }
        }
 
        private void RenderScene()
        {
            // TODO: Add code for rendering your scene here
            imageGraphics.FillRectangle(new SolidBrush(Color.Black),
            this.ClientRectangle);
            //imageGraphics.FillRectangle(new SolidBrush(Color.Blue), image);
            if (lost)
            {
                playMatrix.draw(imageGraphics);
                imageGraphics.DrawString("YOU LOST SUCKER!!!!", new Font("Doktor Terror", 30),
                    Brushes.Red, 100, 300);
            }
            if (won)
                imageGraphics.DrawString("YOU LUCKY BASTARD, YOU WON!!!!", new Font("Doktor Terror", 30),
                    Brushes.Red, 10, 300);
            player.draw(imageGraphics);
            this.BackgroundImage = backBuffer;
            this.Invalidate();

        }
 
        private void GameLogic()
        {
            // TODO: Add code for your game logic here
            if (playMatrix.GetTypOfCell(player.getX(), player.getY()) == 'T')
            {
                lost = true;
            }
            if (playMatrix.GetTypOfCell(player.getX(), player.getY()) == 'X')
            {
                won = true;
            }
        }

        
        private void Spelplan_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Escape)
                contPlay = false;
        }

        private void Spelplan_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Down:
                    player.move(0,1);
                    break;
                case Keys.Right:
                    player.move(1,0);
                    break;
                case Keys.Up:
                    player.move(0,-1);
                    break;
                case Keys.Left:
                    player.move(-1,0);
                    break;
            }
        }

        private void Init()
        {
            menu.show();
            this.DoubleBuffered = true;
            this.MaximizeBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;

            this.ClientSize = new Size(420, 420);
            clientWidth = this.ClientRectangle.Width;
            clientHeight = this.ClientRectangle.Height;
            backBuffer = (Image)new Bitmap(clientWidth, clientHeight);
            graphics = this.CreateGraphics();
            imageGraphics = Graphics.FromImage(backBuffer);
            playMatrix.init();
        }

        private void playGame()
        {
            menu.hide();
            playMatrix.init();
            lost = false;
            won = false;
            
            this.GameLoop();

            menu.show();
            contPlay = true;
            this.Invalidate();
        }
    }
}
